package com.cyparty.mac.quaro.adapter;

import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cyparty.mac.quaro.R;
import com.cyparty.mac.quaro.model.Content;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by macbookpro on 16/8/8.
 */
public class ContentAdapter extends RecyclerView.Adapter {
    static final int ITEM_TYPE_BASE = 0;
    private Context mContext;
    private ArrayList<Content> mContents;

    public ContentAdapter(List<Content> contentList) {
        mContents = (ArrayList<Content>) contentList;
    }

    public void appendContents(List<Content> contexts) {
        if (mContents == null) {
            mContents = new ArrayList<>();
            mContents.addAll(contexts);
        } else {
            mContents.addAll(contexts);
        }
        notifyDataSetChanged();
    }

    public void replaceContents(List<Content> contents) {
        if (mContents == null) {
            mContents = new ArrayList<>();
        } else {
            mContents.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        switch (getItemViewType(viewType)) {
            default:
                return new BaseHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_content_base, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            default:
                BaseHolder baseHolder = (BaseHolder) holder;
                baseHolder.getActionMore().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(mContext, "被点击", Toast.LENGTH_SHORT).show();
                        PopupMenu popupMenu = new PopupMenu(mContext, view);
                        popupMenu.getMenuInflater().inflate(R.menu.item_menu, popupMenu.getMenu());
                        popupMenu.show();
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public class TitleOverlopHolder extends RecyclerView.ViewHolder {

        public TitleOverlopHolder(View itemView) {
            super(itemView);
        }
    }

    public class BaseHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ic_action_more)
        View mActionMore;

        public BaseHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public View getActionMore() {
            return mActionMore;
        }
    }
}
